package com.meida.module.bus.provider.handler;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.meida.common.base.entity.EntityMap;
import com.meida.common.constants.CommonConstants;
import com.meida.common.mybatis.interceptor.PageInterceptor;
import com.meida.common.mybatis.model.PageParams;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.common.security.OpenHelper;
import com.meida.module.order.client.entity.OrderDetails;
import com.meida.module.order.client.entity.OrderInfo;
import com.meida.module.order.provider.service.OrderDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;


/**
 * description: 用户订单列表
 * date: 2022年-06月-24日 11:13
 * author: ldd
 */
@Component("busUserOrderListHandler")
public class UserOrderListHandler implements PageInterceptor<OrderInfo> {
    @Autowired
    private OrderDetailsService orderDetailsService;

    @Override
    public void prepare(CriteriaQuery cq, PageParams pageParams, EntityMap params) {
        Long userId = OpenHelper.getUserId();
        //未删除
        cq.eq(OrderInfo.class, "deleted", CommonConstants.DEL_NO);
        cq.eq(OrderInfo.class, "userId", userId);
        cq.eq(OrderInfo.class, "orderType", 1);
        cq.eq(ObjectUtil.isNotNull(params.get("orderState")), "orderState", params.get("orderState"));
        cq.orderByDesc("createTime");
    }

    @Override
    public void complete(CriteriaQuery<OrderInfo> cq, List<EntityMap> result, EntityMap extra) {
        for (EntityMap orderInfo : result) {
            //查询订单明细
            OrderDetails orderDetails = orderDetailsService.getOne(new QueryWrapper<OrderDetails>().lambda().eq(OrderDetails::getOrderId, orderInfo.getLong("orderId")));
            orderInfo.put("orderDetails", orderDetails);
        }
    }
}
